﻿#include "CAlarmDetailsDlg.h"
#include "CSqlOper.h"
#include "CGlobal.h"
#include "cimagedownloader.h"
#include "Public.h"
#include "CZoomableLabel.h"

CAlarmDetailsDlg::CAlarmDetailsDlg(QString sGuid, QWidget *parent)
	: m_sGuid(sGuid),QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("报警详情"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	// 确保labelImg能够接收事件
	ui.labelImg->setFocusPolicy(Qt::StrongFocus);
	ui.labelImg->setMouseTracking(true);
	setStyleSheetFile();
	initConnect();
	setData();
}

CAlarmDetailsDlg::~CAlarmDetailsDlg()
{}

void CAlarmDetailsDlg::initConnect()
{
	connect(ui.pushButtonAlarmConfrim, &QPushButton::clicked, this, &CAlarmDetailsDlg::slotPushBtnAlarmConfrim);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CAlarmDetailsDlg::slotPushBtnClose);
}

void CAlarmDetailsDlg::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CAlarmDetailsDlg::setData()
{
	QString sErrMsg;
	QVector<QSqlRecord> vecInfo;
	CSqlOper::instance().selectQuertAlarmRecordByGuid(m_sGuid, vecInfo, sErrMsg);
	for (int i = 0; i < vecInfo.size(); i++)
	{
		ui.lineEditCamerName->setText(vecInfo[i].value("DEV_NAME").toString());
		ui.lineEditAlarmLevel->setText(vecInfo[i].value("ALARM_LEVEL").toString());
		ui.lineEditAlarmTime->setText(vecInfo[i].value("ALARM_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));
		ui.lineEditAlarmType->setText(vecInfo[i].value("EVENT_TYPE").toString());
		if (vecInfo[i].value("IS_CONFIRM").toInt() == 1)
		{
			ui.lineEditIsConfrim->setText(A2T("是"));
		}
		else
		{
			ui.lineEditIsConfrim->setText(A2T("否"));
		}
		ui.lineEditAlarmDesc->setText(vecInfo[i].value("ALARM_DESC").toString());
		QString sUrl = vecInfo[i].value("IMG_URL").toString();
		qDebug() << sUrl << endl;
		QString username = vecInfo[i].value("CAMER_USER").toString();
		QString password = vecInfo[i].value("CAMER_PASSWD").toString(); // 替换为实际密码

		// 创建下载器
		CImageDownloader* downloader = new CImageDownloader(ui.labelImg, sUrl, username, password, this);

		// 关键修改：确保使用CZoomableLabel的setPixmap方法
		connect(downloader, &CImageDownloader::downloadFinished, this, [this](const QPixmap& pixmap) {
			qDebug() << A2T("图片下载完成，准备设置到CZoomableLabel");

			// 将ui.labelImg转换为CZoomableLabel
			CZoomableLabel* zoomLabel = qobject_cast<CZoomableLabel*>(ui.labelImg);
			if (zoomLabel) {
				qDebug() << A2T("成功转换为CZoomableLabel，调用setPixmap");
				zoomLabel->setPixmap(pixmap);

				// 确保控件获得焦点，以便接收事件
				zoomLabel->setFocus();
			}
			else {
				qWarning() << A2T("转换CZoomableLabel失败! 实际类型:") << ui.labelImg->metaObject()->className();
				// 备用：直接设置到QLabel
				ui.labelImg->setPixmap(pixmap.scaled(ui.labelImg->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
			}
		});

		connect(downloader, &CImageDownloader::downloadFailed, this, [](const QString& error) {
			qWarning() << "图片下载失败:" << error;
		});

		// 开始下载
		downloader->downloadAsync();
	}
}

void CAlarmDetailsDlg::slotPushBtnAlarmConfrim()
{
	QString sErrMsg = "";
	bool bRet=CSqlOper::instance().updateAlarmRecord(m_sGuid,sErrMsg);
	if (bRet)
	{
		messageBox(0, A2T("报警确认成功"));
	}
	else
	{
		messageBox(1, A2T("报警确认失败，原因:%1").arg(sErrMsg));
	}
}

void CAlarmDetailsDlg::slotPushBtnClose()
{
	this->close();
}
